package com.ysu.shop.controller;

import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.auth0.jwt.JWT;
import com.ysu.shop.exception.NotExistException;
import com.ysu.shop.service.IAddressService;
import com.ysu.shop.pojo.Address;

import com.ysu.shop.utils.Result;
import com.ysu.shop.utils.InitUtil;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.springframework.web.bind.annotation.*;


import lombok.extern.slf4j.Slf4j;

/**
* <p>
    *  前端控制器
    * </p>
*
* @author 耿志龙
* @since 2020-10-01
*/
@RestController
@Slf4j
@RequestMapping("/address")
public class AddressController {

    @Resource
    private IAddressService iAddressService;

    private void judge(HttpServletRequest request,int id){
        QueryWrapper<Address> wrapper = new QueryWrapper<>();
        String token = request.getHeader("Authorization");
        int userId = JWT.decode(token).getClaim("id").asInt();
        wrapper.eq("user_id",userId).eq("id",id);
        if (iAddressService.getOne(wrapper)==null){
           throw new NotExistException();
        }
    }

    /**
    * 进行分页查询
    */
    @GetMapping
    public Result getList(HttpServletRequest request) {
        QueryWrapper<Address> wrapper = new QueryWrapper<>();
        String token = request.getHeader("Authorization");
        int userId = JWT.decode(token).getClaim("id").asInt();
        wrapper.eq("active",1).eq("user_id",userId);
        return Result.getSuccessResult(iAddressService.list());
    }

    /**
    * 通过主键id在表中查找信息
    */
    @GetMapping("/{id}")
    public Result getById(@PathVariable int id) {
        Address iAddress = iAddressService.getById(id);
        if (iAddress == null) {
            return Result.getFailResult("","无记录");
        }
        return Result.getSuccessResult(iAddress);
    }

    /**
    * 向表中添加一条记录
    */
    @PostMapping()
    public Result save(@RequestBody Address iAddress,HttpServletRequest request) {
        QueryWrapper<Address> wrapper = new QueryWrapper<>();
        String token = request.getHeader("Authorization");
        int userId = JWT.decode(token).getClaim("id").asInt();
        iAddress.setUserId(userId);
        if (iAddressService.save(iAddress)){
            return Result.getSuccessResult("","添加成功");
        }
        return Result.getFailResult("","添加失败");
    }

    /**
    * 根据表中的主键修改 根据表中字段相应的修改，如果存在则修改
    */
    @PutMapping("/{id}")
    public Result update(@RequestBody Address iAddress, @PathVariable int id,HttpServletRequest request) {
        judge(request,id);
        iAddress.setId(id);
        if (iAddressService.updateById(iAddress)){
            return Result.getSuccessResult("","修改成功");
        }
        return Result.getFailResult("","修改失败");
    }


    /**
    * 在数据表中真正的删除一条记录， 先根据用户token和id 判断 地址记录是否存在
    */
    @DeleteMapping("/{id}")
    public Result delById(@PathVariable int id,HttpServletRequest request) {
        judge(request,id);
        if (iAddressService.removeById(id)){
            return Result.getSuccessResult("","删除成功");
        }
        return Result.getFailResult("","删除失败");
    }

}
